﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using DAL.Entity;
using DAL.Persistencia;
using BLL.Utils;

namespace NewTierra.Pages.Cliente
{
    public partial class ClienteMarkup : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblCliente.Text = Convert.ToString(Session["nomeCliente"]);

                if (Request.QueryString["IdMk"] != null)
                {
                    BuscaMarkup(Convert.ToInt32(Request.QueryString["IdMk"]));
                }

                PopulaGridMarkup(Convert.ToInt32(Session["IdCliente"]));
                VerificaMarkupMeal();

                //if (Request.QueryString["Tipo"].Equals("novo"))
                //{ btnUpdateCliente.Visible = false; }
                //else
                //{ btnNovoCliente.Visible = false; }

            }
        }

        #region Popular Dados

        protected void PopulaGridMarkup(int IdCliente)
        {
            try
            {
                ClienteMarkupDAL cmd = new ClienteMarkupDAL();

                GridMarkup.DataSource = cmd.ListarTodos(IdCliente);
                GridMarkup.DataBind();

            }
            catch (Exception ex)
            {
                lblMsgCadMarkup.Text = ex.Message;
            }
        }

        #endregion

        #region CRUD Markup

        protected void SalvarMarkup(object sender, EventArgs e)
        {
            try
            {

                if (txtCodMarkup.Text != "")
                {
                    LimpaCampos(this.Controls);
                    lblMsgCadMarkup.Text = "";
                    txtDataInicial.Focus();                    
                    btnNovoMarkup.Text = "Salvar";
                    return;
                }

                ClienteMarkupDAL cmd = new ClienteMarkupDAL();
                Utils ut = new Utils();

                //if (cmd.ObterPorCliente(Convert.ToInt32(Session["IdCliente"])))
                //{
                //    MostrarMsg("Já existe markup cadastrado para esse cliente.");
                //    return;
                //}

                Cliente_Markup cm = new Cliente_Markup();

                cm.Markup_dataInicial = Convert.ToDateTime(txtDataInicial.Text);
                cm.Markup_dataFinal = Convert.ToDateTime(txtDataFinal.Text);

                cm.Markup_hotel = Convert.ToDecimal(ut.pMoneyToIMoney(txtMarkupHotel.Text));

                cm.Markup_descontoHotel = Convert.ToDecimal(txtMarkupDescHotel.Text);
                cm.Markup_servico = Convert.ToDecimal(txtMarkupServico.Text);
                cm.Markup_descontoServico = Convert.ToDecimal(txtMarkupDescServico.Text);
                cm.Cliente_id = Convert.ToInt32(Session["IdCliente"]);

                cm.Markup_seasonal = Convert.ToDecimal(Retorna(txtMarkupSeasonal.Text));
                cm.Markup_meals = Convert.ToDecimal(Retorna(txtMarkupMeal.Text));

                cmd.Salvar(cm);
                MostrarMsg("Registro cadastrado com sucesso.");
                LimpaCampos(this.Controls);
                PopulaGridMarkup(Convert.ToInt32(Session["IdCliente"]));

            }
            catch (Exception ex)
            {
                lblMsgCadMarkup.Text = ex.Message;
            }
        }

        protected void AtualizarMarkup(object sender, EventArgs e)
        {
            try
            {

                ClienteMarkupDAL cmd = new ClienteMarkupDAL();
                Utils ut = new Utils();                

                Cliente_Markup cm = new Cliente_Markup();

                cm.Markup_id = Convert.ToInt32(txtCodMarkup.Text);
                cm.Markup_dataInicial = Convert.ToDateTime(txtDataInicial.Text);
                cm.Markup_dataFinal = Convert.ToDateTime(txtDataFinal.Text);

                cm.Markup_hotel = Convert.ToDecimal(ut.pMoneyToIMoney(txtMarkupHotel.Text));

                cm.Markup_descontoHotel = Convert.ToDecimal(txtMarkupDescHotel.Text);
                cm.Markup_servico = Convert.ToDecimal(txtMarkupServico.Text);
                cm.Markup_descontoServico = Convert.ToDecimal(txtMarkupDescServico.Text);
                cm.Cliente_id = Convert.ToInt32(Session["IdCliente"]);

                cm.Markup_seasonal = Convert.ToDecimal(Retorna(txtMarkupSeasonal.Text));
                cm.Markup_meals = Convert.ToDecimal(Retorna(txtMarkupMeal.Text));

                cmd.Atualizar(cm);
                MostrarMsg("Registro atualizado com sucesso.");
                //LimpaCampos(this.Controls);
                PopulaGridMarkup(Convert.ToInt32(Session["IdCliente"]));

            }
            catch (Exception ex)
            {
                lblMsgCadMarkup.Text = ex.Message;
            }
        }

        protected void ExcluirMarkup(object sender, CommandEventArgs e)
        {
            try
            {
                int myid = Convert.ToInt32(e.CommandArgument);

                ClienteMarkupDAL cmd = new ClienteMarkupDAL();

                Cliente_Markup cm = cmd.ObterPorId(myid);

                cmd.Excluir(cm);
                MostrarMsg("Registro excluído com sucesso.");

                PopulaGridMarkup(Convert.ToInt32(Session["IdCliente"]));
            }
            catch (Exception ex)
            {
                lblMsgCadMarkup.Text = ex.Message;
            }
        }

        protected string Retorna(string valor)
        {
            if (valor.Equals(""))
            {
                return null;
            }
            else
            {
                return valor;
            }
        }
        #endregion

        #region Regras

        protected void VerificaMarkupMeal()
        {
            try
            {

                if (ConfigurationManager.AppSettings["PuxaLogo"].ToString().Equals("Rentamar"))
                {
                    lblMeal.Visible = false;
                    txtMarkupMeal.Visible = false;
                }
                else
                {
                    lblMeal.Visible = true;
                    txtMarkupMeal.Visible = true;
                }
                

            }
            catch (Exception ex)
            {
                MostrarMsg(ex.Message);
            }
        }

        protected void EditarMarkup(object sender, CommandEventArgs e)
        {
            try
            {
                int myid = Convert.ToInt32(e.CommandArgument);

                BuscaMarkup(myid);
            }
            catch (Exception ex)
            {
                lblMsgCadMarkup.Text = ex.Message;
            }
        }

        protected void BuscaMarkup(int IdMarkup)
        {
            try
            {

                btnNovoMarkup.Text = "Novo";

                ClienteMarkupDAL cmd = new ClienteMarkupDAL();

                Cliente_Markup cm = cmd.ObterPorId(IdMarkup);

                txtCodMarkup.Text = cm.Markup_id.ToString();
                txtDataInicial.Text = cm.Markup_dataInicial.ToString();
                txtDataFinal.Text = cm.Markup_dataFinal.ToString();
                txtMarkupHotel.Text = cm.Markup_hotel.ToString();
                txtMarkupDescHotel.Text = cm.Markup_descontoHotel.ToString();
                txtMarkupServico.Text = cm.Markup_servico.ToString();
                txtMarkupDescServico.Text = cm.Markup_descontoServico.ToString();

                txtMarkupSeasonal.Text = cm.Markup_seasonal.ToString();
                txtMarkupMeal.Text = cm.Markup_seasonal.ToString();

            }
            catch (Exception ex)
            {
                lblMsgCadMarkup.Text = ex.Message;
            }
        }

        protected void LimpaCampos(ControlCollection controls)
        {
            foreach (Control ctrl in controls)
            {
                if (ctrl is TextBox)
                {
                    (ctrl as TextBox).Text = string.Empty;
                }
                // ... demais controles que queira limpar

                LimpaCampos(ctrl.Controls);
            }
        }

        protected void MostrarMsg(string Msg)
        {
            ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "call me", "showAutoCloseMessage('" + Msg + "')", true);
        }

        protected void btnLimpar_Click(object sender, EventArgs e)
        {
            LimpaCampos(this.Controls);
        }

        #endregion       

        
    }
}